#!/usr/bin/python
# -*- coding: utf-8 -*-

import sys

from pylot.core import (ENCODING, uprint, Path)
from _pylot.cli.interfaces.cliinterface import CliInterface
from _pylot.cli.pylotoptions import pyLotOptionGroup
from optparse import make_option

class CliMainManager (CliInterface):
  def __init__ (self, handler=None, parser=None):
    CliInterface.__init__(self, handler=handler, parser=parser)

    self.version = '0.1'
    self.description = self.tr_(u'DESC')
    self.usage = 'prog [options]'

    self._createOptions()

  def _createOptions(self):

    self.widget_scheme = ('tabs', 'docks', 'process_and_docks', 'process_and_tabs')

    for dest in self.widget_scheme :
      opt = dest.replace('_', '-')
      action = make_option('--%s' % opt, action='store_true', dest=dest)
      setattr(self, 'action_%s' % dest, action)

    self.level = ('silent', 'verbose', 'debug')
    for dest in self.level :
      opt = dest.replace('_', '-')
      action = make_option('--%s' % opt, action='store_true', dest=dest)
      setattr(self, 'action_%s' % dest, action)

    self.autoclean = ('all', 'no')
    for dest in self.autoclean :
      opt = dest.replace('_', '-')
      action = make_option('--autoclean-%s' % opt, action='store_true', dest=dest)
      setattr(self, 'action_autoclean_%s' % dest, action)

    self.action_run_script = make_option('-s', '--script',
      action='store', dest='script')
    self.action_script_params = make_option('--params',
      action='store', dest='script_params', metavar='"p1 p2"')

  def _createOptionGroups(self):
    group = pyLotOptionGroup(self.parser, u'group_qt')
    self.group_qt = self.addGroup(group)

    group = pyLotOptionGroup(self.parser, u'group_outputs')
    self.group_outputs = self.addGroup(group)

    group = pyLotOptionGroup(self.parser, u'group_scripts')
    self.group_scripts = self.addGroup(group)

  def _fillGroups(self):
    for dest in self.widget_scheme :
      self.group_qt.add_option(getattr(self, 'action_%s' % dest))

    for dest in self.level :
      self.group_outputs.add_option(getattr(self, 'action_%s' % dest))
    for dest in self.autoclean :
      self.group_outputs.add_option(getattr(self, 'action_autoclean_%s' % dest))

    self.group_scripts.add_option(self.action_run_script,
                                  userlist=self.parser.console)
    self.group_scripts.add_option(self.action_script_params,
                                  userlist=self.parser.console)


  def runPreOptions(self, options):
    self.options = options

    for obj, group_id in [
      (self.handler, 'widget_scheme'),
      (self.handler.io, 'level'),
      (self.handler.process, 'autoclean')
      ] :

      group = getattr(self, group_id)
      num = 0
      value = None
      for dest in group :
        if getattr(options, dest) :
          num += 1
          value = dest
      if num > 1 :
        lst = []
        for dest in group :
          opt = '"--%s"' % dest.replace('_', '-')
          lst.append(opt.decode(ENCODING))
        uprint(tr_(u'Please select only one option among %s') % (u', '.join(lst)))
        sys.exit(1)
      else :
        if value :
          setattr(obj, group_id, value.decode(ENCODING))

  def _runOptions(self, options):
    self.autoRunOption(self.action_run_script,
                       self._onActionRunScript)

  def _translate(self):
    self._setTexts([
      (self.action_run_script, tr_(u'Run python script, See pylot script documentation')),
      (self.action_script_params, tr_(u'Pass params to script (params are stored in "args" variable (list of unicodes). Ex: --params="1 a" <=> args = [u"1", u"a"] ')),
      ])

    self._setTexts([
      (self.group_qt, tr_(u'Qt Options')),
      (self.group_outputs, tr_(u"Output options")),
      (self.group_scripts, tr_(u"Script options")),
      ], field=u'title')

  def _onActionRunScript(self):
    params = self.options.script_params
    if params :
      args = params.decode(ENCODING).split()
    else :
      args = []
    self.handler.runScript(Path(self.options.script.decode(ENCODING)).abspath(),
                           args=args)
